package com.peaker.advertisingpro.view

import android.content.Context
import android.graphics.Color
import android.os.Handler
import android.view.LayoutInflater
import android.widget.LinearLayout
import android.widget.RelativeLayout
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.peaker.advertisingpro.R
import com.peaker.advertisingpro.bean.CityBean
import com.peaker.advertisingpro.bean.Weather
import com.peaker.advertisingpro.listener.RequestListener
import com.peaker.advertisingpro.utils.DateUtils
import com.peaker.advertisingpro.utils.HttpUtil
import kotlinx.android.synthetic.main.weather_item_layout.view.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

/**
 *  @ 作者 CODE_LYF
 *  @ 时间   2021/4/15.
 *  代码人生 天气对象
 */
class WeatherView(var context: Context, var rootView: RelativeLayout) {
    private val view: LinearLayout =
        LayoutInflater.from(context).inflate(R.layout.weather_item_layout, null) as LinearLayout


    fun setView(width: Int, height: Int,left: Int, top: Int,elevation :Float) {
        val layoutParams = RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT
        )
        layoutParams.width = width
        layoutParams.height = height
        layoutParams.leftMargin =left
        layoutParams.topMargin =top
        view.layoutParams = layoutParams
        view.elevation =elevation
        rootView.addView(view)
        var a: Int = width / 400
        if (a == 0) {
            a = 1
        }
        setViewSize(a.toFloat())

    }

    fun setWeatherData(bean: Weather) {
        if (bean.lives?.size == 0) {
            return
        }
        view.city_tv.text = bean.lives?.get(0)?.city
        view.update_time.text = DateUtils.transferFormat(bean.lives?.get(0)?.reporttime)
        view.weather_img.setImageResource(R.drawable.anothing)
        view.city_weather.text = bean.lives?.get(0)?.weather
        view.celsius_tv.text = bean.lives?.get(0)?.temperature + "℃"
        view.celsius_tv.setTextColor(Color.BLACK)
        view.humidity.text = bean.lives?.get(0)?.humidity + "%"
        view.wdenZh.text = bean.lives?.get(0)?.winddirection + "风"
        view.wdsp.text = bean.lives?.get(0)?.windpower + "级"
        setWeatherImg(bean.lives?.get(0)?.weather!!)
    }

    private fun setWeatherImg(weather: String) {


        if (weather.contains("云")) {
            view.weather_img.setImageResource(R.drawable.a116)
        }
        if (weather.contains("风")) {
            view.weather_img.setImageResource(R.drawable.a260)
        }
        if (weather.contains("雪")) {
            view.weather_img.setImageResource(R.drawable.a338)
        }
        if (weather.contains("雨")) {
            view.weather_img.setImageResource(R.drawable.a308)
        }
        if (weather.contains("雾")) {
            view.weather_img.setImageResource(R.drawable.a248)
        }
        when (weather) {
            "晴" -> view.weather_img.setImageResource(R.drawable.a113)
            "阴" -> view.weather_img.setImageResource(R.drawable.a122)
            else -> {
               // view.weather_img.setImageResource(R.drawable.anothing)

            }

        }
    }

    fun setViewSize(per: Float) {
        view.city_tv.textSize = 12 * per
        view.update_time.textSize = 12 * per
        view.update_tv.textSize = 12 * per
        var layoutParams = view.weather_img.layoutParams
        layoutParams.height = 30 * per.toInt()
        layoutParams.width = 60 * per.toInt()
        view.weather_img.layoutParams = layoutParams
        var layoutParams1 = view.positioning_icon.layoutParams
        layoutParams1.height = 15 * per.toInt()
        layoutParams1.width = 15 * per.toInt()
        view.positioning_icon.layoutParams = layoutParams1
        view.city_weather.textSize = 12 * per
        view.celsius_tv.textSize = 27 * per
        var layoutParams2 = view.humidity_icon.layoutParams
        layoutParams2.height = 15 * per.toInt()
        layoutParams2.width = 15 * per.toInt()
        view.humidity_icon.layoutParams = layoutParams2
        view.humidity_tv.textSize = 12 * per
        view.humidity.textSize = 12 * per
        var layoutParams3 = view.wind_icon.layoutParams
        layoutParams3.height = 15 * per.toInt()
        layoutParams3.width = 15 * per.toInt()
        view.wind_icon.layoutParams = layoutParams3
        view.wdenZh.textSize = 12 * per
        view.wdsp.textSize = 12 * per
    }

    fun startHttp() {
        var paramsMap = HashMap<String, String>()
        HttpUtil.requestPostByAsyn(
            "https://restapi.amap.com/v3/ip?key=b9b33738f6f739e81e375208387bd4f6",
            paramsMap,
            object : RequestListener {
                override fun OnRequestSuccess(data: String) {

                    val type = object : TypeToken<CityBean>() {}.type
                    val city = Gson().fromJson<CityBean>(data, type)
                    println("天气$data")
                    gethttp(city.adcode)
                }

                override fun OnRequestErro(data: String) {
                    println("天气$data")
                }
            })
    }

    private fun gethttp(infocode: String?) {
        var paramsMap = HashMap<String, String>()
        HttpUtil.requestPostByAsyn(
            "https://restapi.amap.com/v3/weather/weatherInfo?city=${infocode}&key=b9b33738f6f739e81e375208387bd4f6",
            paramsMap,
            object : RequestListener {
                override fun OnRequestSuccess(data: String) {
                    val type = object : TypeToken<Weather>() {}.type
                    val data = Gson().fromJson<Weather>(data, type)
//                    setWeatherData(data)
                    println("天气$data")
                    GlobalScope.launch(Dispatchers.Main) {
                        setWeatherData(data)
                    }
                }

                override fun OnRequestErro(data: String) {
                    println("天气$data")
                }
            })
    }

    fun startHttpWeather() {
        val handler = Handler()
        val runnable: Runnable = object : Runnable {
            override fun run() {
                //要做的事情
                startHttp()
                handler.postDelayed(this, 10 * 60 * 1000)
            }
        }
        handler.postDelayed(runnable, 10 * 60 * 1000)

    }
    fun  getView()=view


}